package com.yiip.flume.interceptor;

import org.apache.flume.Context;
import org.apache.flume.Event;
import org.apache.flume.interceptor.Interceptor;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class LogTypeInterceptor implements Interceptor {
    @Override
    public void initialize() {

    }

    @Override
    public Event intercept(Event event) {
        //1、取到日志内容
        byte[] body = event.getBody();
        String content = new String(body, Charset.forName("UTF-8"));
        Map header = event.getHeaders();
        if(content.contains("start")){
            header.put("topic","topic_start");
        }else {
            header.put("topic","topic_event");
        }

        return null;
    }

    @Override
    public List<Event> intercept(List<Event> events) {
        List list = new ArrayList();
        for (Event event : events) {
            Event eventItem = intercept(event);
            if(eventItem != null){
                list.add(eventItem);
            }
        }
        return list;
    }


    @Override
    public void close() {

    }

    public static class Builder implements Interceptor.Builder{

        @Override
        public Interceptor build() {
            return new LogTypeInterceptor();
        }

        @Override
        public void configure(Context context) {

        }
    }
}
